package com.epalmpay.controller.channel.mall;

import com.epalmpay.commom.BaseController;
import com.epalmpay.status.Rescode;
import com.epalmpay.dto.mall.FreightDetailDTO;
import com.epalmpay.dto.mall.FreightTemplateInDTO;
import com.epalmpay.dto.mall.FreightTemplateOutDTO;
import com.epalmpay.dto.system.BaseOutDTO;
import com.epalmpay.service.mall.IFreightTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * Created by lifayi on 2017/8/3.
 */
@RestController
@RequestMapping("/merchant/mall")
public class FreightTemplateController  extends BaseController{

    @Autowired
    private IFreightTemplateService freightTemplateService;

    /*
    *  运费模板列表
    * */
    @RequestMapping(value = "/listFreightTemplate",method = RequestMethod.POST)
    public List<FreightTemplateOutDTO> listFreightTemplate(@RequestBody Long groupId) {
        try {
            return freightTemplateService.listFreightTemplate(groupId);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /*
    *  运费模板列表接口
    * */
    @RequestMapping(value = "/listFreightTemplate",method = RequestMethod.GET)
    public List<FreightTemplateOutDTO> listFreightTemplate1(@RequestParam Long groupId) {
        try {
            List<FreightTemplateOutDTO> freightTemplateOutDTOList = freightTemplateService.listFreightTemplate(groupId);
            FreightTemplateOutDTO freightTemplateOutDTO = new FreightTemplateOutDTO();
            freightTemplateOutDTO.setId(-2L);
            freightTemplateOutDTO.setName("自提");
            freightTemplateOutDTOList.add(freightTemplateOutDTO);
            FreightTemplateOutDTO freightTemplateOutDTO2 = new FreightTemplateOutDTO();
            freightTemplateOutDTO2.setId(-1L);
            freightTemplateOutDTO2.setName("免运费");
            freightTemplateOutDTOList.add(freightTemplateOutDTO2);
            return freightTemplateOutDTOList;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /*
    *  运费地区详情
    * */
    @RequestMapping("/freightDetail")
    public List<FreightDetailDTO> freightDetail(@RequestBody Map<String, Long> info) {
        Long freightId = info.get("freightId");
        Long groupId = info.get("groupId");
        try {
            return freightTemplateService.freightDetail(freightId, groupId);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /*
    *  运费模板删除
    * */
    @RequestMapping("/freightDel")
    public BaseOutDTO freightDel(@RequestBody Map<String, Long> info) {
        Long freightId = info.get("freightId");
        Long groupId = info.get("groupId");
        return freightTemplateService.freightDel(freightId, groupId);
    }

    /*
    *  运费详情
    * */
    @RequestMapping("/freightTemplateDetail")
    public FreightTemplateOutDTO freightTemplateDetail(@RequestBody Map<String, Long> info) {
        Long freightId = info.get("freightId");
        Long groupId = info.get("groupId");
        try {
            return freightTemplateService.freightTemplateDetail(freightId, groupId);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /*
    * 添加或者修改运费模板
    * */
    @RequestMapping("/freightAdd")
    public BaseOutDTO freightAdd(@RequestBody FreightTemplateInDTO freightTemplateInDTO) {
        try {
            return freightTemplateService.freightAdd(freightTemplateInDTO);
        } catch (Exception e) {
            e.printStackTrace();
            BaseOutDTO baseOutDTO = new BaseOutDTO();
            baseOutDTO.setErrorCode(Rescode.SUCCESS);
            return baseOutDTO;
        }
    }

    /*
    * 获取所有模板名称和ID
    * */
    @RequestMapping(value = "/listAllFreightNameAndId")
    @ResponseBody
    public List<Map<String, Object>> listAllFreightNameAndId(@RequestBody Long groupId) {
        return freightTemplateService.listAllFreightNameAndId(groupId);
    }
}
